package threaddemo;

public class demo5 {
    /**
     * 死锁
     */
    private static Object locker1 = new Object();
    private static Object locker2 = new Object();
    public static void main(String[] args) throws InterruptedException {
        Thread t1 = new Thread(() -> {
            synchronized (locker1) {
                try {
                    Thread.sleep(1000);  //防止t1线程直接获取到locker2锁
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                synchronized (locker2) {}
            }
        }, "t1");
        Thread t2 = new Thread(() -> {
            synchronized (locker2) {
                try {
                    Thread.sleep(1000);//防止t2线程直接获取到locker1锁
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                synchronized (locker1) {}
            }
        }, "t2");
        t1.start();
        t2.start();
        t1.join();
        t2.join();
        System.out.println("结束");
    }
}
